﻿/*
 * 文件名称：
 * 程序员：huangsb
 * 创建时间：2008/05/20
 * 
 * 功能说明：帐号注册、修改
 * 
 * 修改人员：
 * 
 * 修改日期：
 * 
 * 修改记录：
 */

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Whir.Common.WebHelper;

public partial class Whir_System_Module_Role_AddAdmin : System.Web.UI.Page
{
    protected string id = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        RoleHelper.IsLogin();    //登录判断

        id = NetRequest.GetQueryString("id");

        if (!IsPostBack)
        {
            this.rblTypeID.Attributes.Add("onclick", "toStart();");

            setData();

            if (string.IsNullOrEmpty(id))
            {
                this.pwd1.Visible = false;
                if (RoleHelper.AdminCache.typeID != 1)
                {
                    JsHelper.redirect(Page, "当前用户不是超级管理员，无法继续操作！", "../../Main.aspx");
                }
            }
            else
            {
                this.RequiredFieldValidator1.Enabled = false;
                this.RequiredFieldValidator2.Enabled = false;
            }

            if (RoleHelper.AdminCache.typeID != 1)
            {
                trAdmin.Visible = false;
                trRole.Visible = false;
            }
        }
    }

    //绑定左边菜单
    protected void BindRbl(ListBox lb, string strWhere)
    {
        DataTable dt = Whir.SQLServerDAL.DbHelperSQL.GetDataTable("RoleID,RoleName", "Whir_AdminRole", strWhere, "");

        lb.DataSource = dt.DefaultView;
        lb.DataValueField = "RoleID";
        lb.DataTextField = "RoleName";
        lb.DataBind();
    }

    //初始化
    protected void setData()
    {
        BindRbl(this.lb1, "");

        if (this.id != "")
        {
            Whir.Model.Whir_AdminInfo model = new Whir.SQLServerDAL.Whir_AdminInfo().GetModel(int.Parse(this.id));

            this.txtAdminName.Text = model.adminName;
            this.txtAdminName.Enabled = false;
            this.txtAdminTrueName.Text = model.adminTrueName;
            this.hfPwd.Value = model.adminPassword;
            this.rblTypeID.SelectedValue = model.typeID.ToString();
            this.hf2.Value = model.roleIDs;

            if (model.roleIDs != "")
                BindRbl(this.lb2, "roleID in (" + model.roleIDs + ")");
        }
    }

    //提交
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        //判断帐号是否已存在
        DataTable dt = Whir.SQLServerDAL.DbHelperSQL.GetDataTable("count(*)", "Whir_AdminInfo", "AdminName='" + this.txtAdminName.Text + "'", "");

        if (string.IsNullOrEmpty(this.id))
        {
            if (dt.Rows[0][0].ToString() != "0")
            {
                Whir.Common.WebHelper.JsHelper.Alert(Page, "该帐号已经存在，请换一个新帐号!");
                return;
            }
        }
        //end

        Whir.SQLServerDAL.Whir_AdminInfo bll = new Whir.SQLServerDAL.Whir_AdminInfo();
        Whir.Model.Whir_AdminInfo model = new Whir.Model.Whir_AdminInfo();
        if (this.id != "")
            model = bll.GetModel(int.Parse(this.id));

        model.adminName = this.txtAdminName.Text;
        model.adminTrueName = this.txtAdminTrueName.Text;
        //密码
        if (this.id != "")
        {
            if (this.txtAdminPassword.Text != "")
                model.adminPassword = Whir.Common.WebHelper.StringHelper.GetSHA1Str(this.txtAdminPassword.Text);
        }
        else
            model.adminPassword = Whir.Common.WebHelper.StringHelper.GetSHA1Str(this.txtAdminPassword.Text);

        model.typeID = int.Parse(this.rblTypeID.SelectedValue);
        model.roleIDs = Request.Form["hf2"].ToString();
        model.roleValue = "";    //角色值[虚值]

        if (this.id == "")
        {
            model.createDate = DateTime.Now;
            model.createUserID = RoleHelper.GetAdminID();
            bll.Add(model);
        }
        else
        {
            bll.Update(model);
        }

        Response.Redirect("AdminList.aspx");
    }
}
